package lg.minecraft.plugin.cobblemoncore.config.mapping;

import lg.minecraft.plugin.cobblemoncore.LGCobblemonCore;
import lg.minecraft.plugin.library.config.LGBaseConfig;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * @author langle__
 * @version 1.0
 */
@Getter
public abstract class AbstractMapping<T> extends LGBaseConfig {

    protected Map<String, T> mapping;
    protected Map<T, String> reverse;

    public AbstractMapping(LGCobblemonCore plugin, String fileName, String directoryPath) {
        super(plugin, fileName, directoryPath);
    }

    @Override
    public void load() {
        this.mapping = new HashMap<>();
        this.reverse = new HashMap<>();
        if (isUseLocalFile()) {
            if (!getFile().exists()) {
                loadFromDynamicSource();
                saveDefault();
            } else {
                super.load();
                loadFromLocalFile();
            }
        } else {
            // 动态获取数据，忽略本地文件
            loadFromDynamicSource();
        }
    }

    protected abstract boolean isUseLocalFile();

    /**
     * 初始化映射数据，从动态数据源加载
     */
    protected abstract void loadFromDynamicSource();

    /**
     * 初始化映射数据，从本地文件加载
     */
    protected abstract void loadFromLocalFile();

    @Override
    public void saveDefault() {
        if (!getFile().exists()) {
            save();
        }
    }

}